<?php
/**
 * Enum class for all available minimum periods for reservations
 *
 * @access 	public
 * @author 	Rob
 * @copyright	ResMania 2009 all rights reserved.
 * @version	1.0
 * @link		http://developer.resmania.com/api
 * @since  	05-2009
 */
class RM_DayRent_Period {
    const flexible = 0;
    const night_1 = 1;
    const nights_2 = 2;
    const nights_3 = 3;
    const nights_4 = 4;
    const nights_5 = 5;
    const nights_6 = 6;
    const week_1 = 7;
    const weeks_2 = 14;
    const weeks_3 = 21;
    const month_1 = 30;
    const months_2 = 60;
    const months_3 = 90;

    public static $periods = array(
    	self::flexible => 'Flexible',
    	self::night_1 => 'OneNight',
    	self::nights_2 => 'TwoNights',
    	self::nights_3 => 'ThreeNights',
    	self::nights_4 => 'FourNights',
    	self::nights_5 => 'FiveNights',
    	self::nights_6 => 'SixNights',
    	self::week_1 => 'OneWeek',
    	self::weeks_2 => 'TwoWeeks',
    	self::weeks_3 => 'ThreeWeeks',
    	self::month_1 => 'OneMonth',
    	self::months_2 => 'TwoMonths',
    	self::months_3 => 'ThreeMonths'
    );

    const languageSection = 'Admin.DayRent.Periods';

    /**
     * Returns all translated periods.
     *
     * @param Zend_Translate $translate OPTIONAL If don't provide method will use default one from Zend_Registry
     * @return array
     */
    public static function getAllTranslated($translate = null)
    {
        if ($translate == null) {
            $translate = RM_Environment::getInstance()->getTranslation(RM_Environment::TRANSLATE_MAIN);
        }
        $section = self::languageSection;
        $result = array();

        foreach (self::$periods as $id => $name){
            $result[$id] = $translate->_($section, $name);
        }
        return $result;
    }    

    /**
     * Return period name (need to be translated using )
     *
     * @param Int $key perido key
     * @return String Thranslate constant
     */
    public static function getPeriodName($key)
    {
    	return self::$periods[$key];
    }

    /**
     * @return RM_Date $start
     */
    function getStart($period){
        return $period->getStart();
    }

    /**
     * @return RM_Date $start
     */
    function getEnd($period){
        return $period->getEnd();
    }
    
    /**
     * Calculate period length in days
     *
     * @param $days boolean - return in
     * @return int - length is days
     */
    public function getLength($period, $round){
        $endTimestamp = $this->getEnd($period)->toString('U');
        $startTimestamp = $this->getStart($period)->toString('U');
        $hoursNumber = ($endTimestamp - $startTimestamp) / (60 * 60);
        
        if ($hoursNumber<=24) return 1; // one day is the smallest
        
        if ($round->roundup || $round->rounddown){
        
            if ($round->roundup && $round->rounddown){
                return round($hoursNumber / 24);
            }

            if ($round->roundup){
                return ceil($hoursNumber / 24);
            }

            if ($round->rounddown){
                return floor($hoursNumber / 24);
            }
            
        }

        return round($hoursNumber / 24);
    }

    /**
     * Returns array of days between two dates. Including both first
     * and last days
     *
     * @param $excluding int -
     * 0: no exclude - including both first and last
     * 1: excluding last
     * 2: excluding first
     * 3: excluding both first and last
     *
     * @return array Array with RM_Date objects
     */
    public function getDays($period, $excluding, $roundup = false){
        $result = array();
        $startDate = clone $period->getStart($period);

        $numberOfDays = $this->getLength($period, $roundup); // we need to include one last day

        for ($i = 1; $i <= $numberOfDays; $i++) {
            $result[] = clone $startDate;
            $startDate->addDay(1);
        }
        return $result;
    }
}